/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        role_id: uinfo.role_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        //h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].role_name + '</td>';
        h += '<td><img src="' + d[i].head + '" height="30"/></td>';
        h += '<td>' + d[i].shop_name + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'staff-edit.html\', ' + d[i].id + ');">';
        h += '编辑';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '&nbsp;&nbsp;';
        if (d[i].is_able > 0) {
            h += '<a href="javascript:;" class="ml-5" onclick="lay_able_yes(' + d[i].id + ');">';
            h += '启用';
            h += '</a>';
        } else {
            h += '<a href="javascript:;" class="ml-5" onclick="lay_able_no(' + d[i].id + ');" style="color: red;">';
            h += '禁用';
            h += '</a>';
        }
        h += '<br/>';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_edit_pwd(\'修改密码\',\'staff-pwd.html\', ' + d[i].id + ');">';
        h += '修改密码';
        h += '</a>';
        //h += '&nbsp;&nbsp;';
        //h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        //h += ' 删除';
        //h += '<i class="Hui-iconfont"></i>';
        //h += '</a>';
        h += '<input type="hidden" id="title' + d[i].id +'" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="head' + d[i].id +'" value="' + d[i].head + '"/>';
        h += '<input type="hidden" id="role_id' + d[i].id +'" value="' + d[i].role_id + '"/>';
        h += '<input type="hidden" id="shop_id' + d[i].id +'" value="' + d[i].shop_id + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//选项
function getOption() {
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/option',
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg);
                return
            }
            var role = res.data.role, shop = res.data.shop;
            var h = '', i = 0;
            //角色
            h = '';
            if (!role_id) {
                h += '<option value="" selected>-选择-</option>';
            } else {
                h += '<option value="">-选择-</option>';
            }
            for (i = 0; i < role.length; i ++) {
                if (role_id == role[i].id) {
                    h += '<option value="' + role[i].id + '" selected>';
                } else {
                    h += '<option value="' + role[i].id + '">';
                }
                h += role[i].title;
                h += '</option>';
            }
            $('#role_id').html(h);
            //门店
            h = '';
            if (!shop_id) {
                h += '<option value="" selected>-选择-</option>';
            } else {
                h += '<option value="">-选择-</option>';
            }
            for (i = 0; i < shop.length; i ++) {
                if (shop_id == shop[i].id) {
                    h += '<option value="' + shop[i].id + '" selected>';
                } else {
                    h += '<option value="' + shop[i].id + '">';
                }
                h += shop[i].title;
                h += '</option>';
            }
            $('#shop_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var head = $('#head' + id).val();
    var role_id = $('#role_id' + id).val();
    var shop_id = $('#shop_id' + id).val();
    tt = tt ? escape(tt) : '';
    head = head ? head : '';
    role_id = role_id ? role_id : 0;
    shop_id = shop_id ? shop_id : 0;
    url += '&title=' + tt;
    url += '&head=' + head;
    url += '&role_id=' + role_id;
    url += '&shop_id=' + shop_id;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    $('#title')[0].value = title;
    var h = '';
    if (head != '') {
        $('#bigShow').html('<img src="' + head + '"/>');
    }
    getOption();
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.img + '" style="width: 150px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//修改
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
        head: $('#img').val(),
        role_id: $('#role_id').val(),
        shop_id: $('#shop_id').val(),
    };
    if (formData.title == '') {
        msgPop({msg: '姓名必须的'}); return
    }
    if (formData.head == '') {
        msgPop({msg: '头像必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//启用
function lay_able_yes(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">启用</b> 吗？';
    layer.confirm(msg, function (index) {
        toable({id: id, able: 0});
    });
}


//禁用
function lay_able_no(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">禁用</b> 吗？';
    layer.confirm(msg, function (index) {
        toable({id: id, able: 1});
    });
}


//禁用启用
function toable(e) {
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/toable',
        data: {  id: e.id, able: e.able },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            layer.msg('成功!', {icon: 1, time: 1000});
            setTimeout(function () {
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


////删除
//function lay_del(id) {
//    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
//    layer.confirm(msg, function (index) {
//        $.ajax({
//            type: apiMethod,
//            url: apiDomain + 'yw-mall/shop/del',
//            data: {  id: id },
//            success: function (res) {
//                console.log(res);
//                if (res.code < 0) {
//                    msgPop({msg: res.msg});
//                    return
//                }
//                layer.msg('已删除!', {icon: 1, time: 1000});
//                setTimeout(function () {
//                    top.location.reload();
//                }, 500);
//            },
//            error: function () {
//                console.log('错误了！');
//            }
//        });
//    });
//}


//密码弹窗
function lay_edit_pwd(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    tt = tt ? escape(tt) : '';
    url += '&title=' + tt;
    h = 300;
    layer_show(title,url,w,h);
}


//重置密码
function lay_pwd_reset() {
    console.log(id);
    var msg = '确认要 <b style="color: red; font-size: 20px;">重置</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'staff/reset',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg);
                    return
                }
                layer.msg('成功!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//修改密码
function lay_pwd_modify(id) {
    var formData = {
        id: id,
        pwd: $('#pwd').val(),
        pwd2: $('#pwd2').val(),
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/pwdmodify',
        data: {  id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            layer.msg('成功!', {icon: 1, time: 1000});
            setTimeout(function () {
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}